
var table1 = {
    id: "table1",	//表格id
    table: null,
    layerIndex: -1
};
var table2 = {
    id: "table2",	//表格id
    table: null,
    layerIndex: -1
};


var ids=new Array();
/**
 * 初始化表格的列
 */
table1.initColumn = function () {
    return [
        //{field: 'selectItem', radio: false},
        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: '上游网点', field: 'send_dept_name', visible: true, align: 'center', valign: 'middle'},
        {title: '下游网点', field: 'transfer_name', visible: true, align: 'center', valign: 'middle'},
        {title: '单号', field: 'original_num', visible: true, align: 'center', valign: 'middle'},
        {title: '回单要求', field: 'receipt', visible: true, align: 'center', valign: 'middle' },
        {title: '回单状态', field: 'receipt_status', visible: true, align: 'center', valign: 'middle',
            formatter : function(value,row,index){
            if(row.receipt_status=='0'){
                return "待办";
            }else  if(row.receipt_status=='1'){
                return "已收";
            }else if(row.receipt_status=='2'){
                    return "已完成";
            }else if(row.receipt_status=='3'){
                return "无需处理";
            }
        }  },
        {title: '运单类型', field: '', visible: true, align: 'center', valign: 'middle',
            formatter : function(value,row,index){
            if(row.is_transfer=='1'){
                    return "中转";
            }else {
                if(row.mode=='0'){
                    return "自提";
                }else if(row.mode=='1'){
                    return "配送";
                }
            }
        }
        },

        {title: '收货人', field: 'consignee', visible: true, align: 'center', valign: 'middle'},
        {title: '货物名称', field: 'goods_name', visible: true, align: 'center', valign: 'middle'},
        {title: '件数', field: 'goods_piece', visible: true, align: 'center', valign: 'middle' },
        {title: '入库时间', field: 'createtime', visible: true, align: 'center', valign: 'middle' },
        {title: '中转地址', field: 'recAddress', visible: true, align: 'center', valign: 'middle' },
        {title: '接收时间', field: 'receive_time', visible: true, align: 'center', valign: 'middle'},
        {title: '发送时间', field: 'send_time', visible: true, align: 'center', valign: 'middle'},
        {title: '发送方式', field: 'process_method', visible: true, align: 'center', valign: 'middle'}

    ];
};
Array.prototype.remove = function(val) {
    var index = this.indexOf(val);
    if (index > -1) {
        this.splice(index, 1);
    }
};

/**
 * 检查表格2是否选中
 */
table1.check = function () {
    var selected = $('#table2').bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中右侧表格中的记录！");
        return false;
    }else{
       return true;
    }
};


table1.orderReceive=function () {

    var selected=$('#table2').bootstrapTable('getData', true);
    if(selected.length<=0){
        Feng.info("请双击选择左边表格中的数据！");
        return false;
    }
        for(var i=0;i<selected.length;i++){
            if(selected[i].receipt_status!=0){
                Feng.info("只能选择待办订单！");
                return false;
            }
        }

        var index = layer.open({
            type: 2,
            shade: 0.01,
            title: '回单接收',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/orderBack/receive?ids='+ids
        });
        this.layerIndex = index;

}

//回单发送
table1.orderSend=function () {
    var selected=$('#table2').bootstrapTable('getData', true);
    if(selected.length<=0){
        Feng.info("请双击选择左边表格中的数据！");
        return false;
    }
        for(var i=0;i<selected.length;i++){
            if(selected[i].receipt_status!=1){
                Feng.info("只能选择已收订单！");
                return false;
            }
        }
        var index = layer.open({
            type: 2,
            shade: 0.01,
            title: '回单发送',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/orderBack/send?ids='+ids
        });
        this.layerIndex = index;
}

//一键收发
table1.orderReceiveAndSend=function () {
    var selected=$('#table2').bootstrapTable('getData', true);
     if(selected.length<=0){
         Feng.info("请双击选择左边表格中的数据！");
         return false;
     }
        for(var i=0;i<selected.length;i++){
            if(selected[i].receipt_status!=0){
                Feng.info("直能选择待办订单");
                return false;
            }
        }

        var index = layer.open({
            type: 2,
            shade: 0.01,
            title: '一键收发',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/orderBack/receAndSend?ids='+ids
        });
        this.layerIndex = index;
}
table1.search = function () {
    var queryData = {};
    queryData['name'] = $("#name").val();
    queryData['type'] = $("#type").val();
    queryData['is_out'] = $("#is_out").val();
    queryData['beginTime'] = $("#beginTime").val();
    queryData['endTime'] = $("#endTime").val();
    queryData['receipt_status'] = $("#receipt_status").val();
    table1.table.refresh({query: queryData});
};

table1.exportOrder = function () {
    var name=$("#name").val()+"";
    var type=$("#type").val()+"";
    var is_out=$("#is_out").val()+"";
    var beginTime=$("#beginTime").val()+"";
    var endTime=$("#endTime").val()+"";
    var receipt_status=$("#receipt_status").val()+"";
    location.href=Feng.ctxPath + "/orderBack/exportOrderBackist?name="+name+'&type='+type+'&is_out='+is_out
        +'&beginTime='+beginTime+'&endTime='+endTime+'&receipt_status='+receipt_status;

};
function onDblClickRow(row){
    $("#table2").bootstrapTable('insertRow', {index:1, row:row});
    $("#table1").bootstrapTable('remove',  {field: 'id', values: [row.id]});
    ids.push(row.id);

}

function removeRow(row){
    $("#table2").bootstrapTable('remove', {field: 'id', values: [row.id]});
    $("#table1").bootstrapTable('insertRow', {index:1, row:row});
    ids.remove(row.id);
}


function removeAll(){
    $("#table2").bootstrapTable('removeAll');
}

function loadData(data){
    var selected=$('#table2').bootstrapTable('getData', true);
    for(var i=0;i<selected.length;i++){
        for(var j=0;j<data.rows.length;j++){
            if(data.rows[j].id==selected[i].id){
                $("#table1").bootstrapTable('remove',  {field: 'id', values: [selected[i].id]});
            }
        }
    }
}

$(function () {
    var data=null;
    var defaultColunms = table1.initColumn();
    var mTable = new BSTable(table1.id, "/orderBack/list", defaultColunms);
    mTable.setDblClickRow(onDblClickRow);
    mTable.setOnLoadSuccess(loadData);
    mTable.init();
    table1.table = mTable;

    var table = new BSTable(table2.id, "", defaultColunms);
    table.setDblClickRow(removeRow);
    table.init();
    table2.table = table;

    layui.use('layer', function () { })

    $('#type').selectpicker({
        style: 'btn btn-select',
        noneSelectedText:"运单类型"
    });
    $('#is_out').selectpicker({
        style: 'btn btn-select',
        noneSelectedText:"运单状态"
    });
    $('#receipt_status').selectpicker({
        style: 'btn btn-select',
        noneSelectedText:"回单状态"
    });
});

$(document).keyup(function(event) {
    if (event.keyCode == 13) {
        table1.search();
    }
});
